package com.shudingke.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shudingke.domain.Cart;
import com.shudingke.domain.Products;
import com.shudingke.domain.vo.CartVo;
import com.shudingke.service.ICartService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-06-29
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    ICartService service;
    
    //前往购物车页面需要经过的方法
    //先从session域中获取到用户的id   如果没有获取到id表明该用户还没有登录
    @RequestMapping("/goToCart")
    public String goToCart(@RequestParam(defaultValue = "")String searchName, Model model,
                         HttpServletRequest req,
                         @RequestParam(defaultValue = "1") String page){
        req.setAttribute("searchName",searchName);
        return "cart";
    }

    
    @ResponseBody
    @RequestMapping("/cartLoad")
    public Map cartLoad(@RequestParam(defaultValue = "")String searchName, Model model,
                           HttpServletRequest req,
                           @RequestParam(defaultValue = "1") String page){
        Map map = new HashMap();
        map.put("searchName",searchName);
        if (StringUtils.isEmpty(searchName)){
            searchName = null;
        }else {
            searchName = "%" + searchName + "%";
        }
        String uuid = req.getSession().getAttribute("uid").toString();
        int uid = Integer.parseInt(uuid);
        List<CartVo> cartList = service.findAll(uid,searchName);
        
        map.put("cartList",cartList);
        
        return map;
    }

    @ResponseBody
    @RequestMapping("/add")
    public Map add(@RequestParam(defaultValue = "")String searchName,
                        HttpServletRequest req,
                        @RequestParam(defaultValue = "1") String page,String cid){
        Map map = new HashMap();
        map.put("searchName",searchName);
        Cart cart = new CartVo();
        cart.setCid(Integer.parseInt(cid));
        int num = cart.selectById().getNum();
        if (num<99999){
            cart.setNum(++num);
        }else {
            cart.setNum(num);
        }
        cart.updateById();
        if (StringUtils.isEmpty(searchName)){
            searchName = null;
        }else {
            searchName = "%" + searchName + "%";
        }
        String uuid = req.getSession().getAttribute("uid").toString();
        int uid = Integer.parseInt(uuid);
        List<CartVo> cartList = service.findAll(uid,searchName);
       
        map.put("cartList",cartList);
        
        return map;
    }

    @ResponseBody
    @RequestMapping("/sub")
    public Map sub(@RequestParam(defaultValue = "")String searchName,
                   HttpServletRequest req,
                   @RequestParam(defaultValue = "1") String page,String cid){
        Map map = new HashMap();
        map.put("searchName",searchName);
        Cart cart = new CartVo();
        cart.setCid(Integer.parseInt(cid));
        int num = cart.selectById().getNum();
        if (num>0){
            cart.setNum(--num);
        }else {
            cart.setNum(num);
        }
        cart.updateById();
        if (StringUtils.isEmpty(searchName)){
            searchName = null;
        }else {
            searchName = "%" + searchName + "%";
        }
        String uuid = req.getSession().getAttribute("uid").toString();
        int uid = Integer.parseInt(uuid);
        List<CartVo> cartList = service.findAll(uid,searchName);
        map.put("cartList",cartList);
        
        return map;
    }


    @ResponseBody
    @RequestMapping("/calculate")
    public Map calculate(@RequestParam(defaultValue = "")String searchName,
                   HttpServletRequest req,
                   @RequestParam(defaultValue = "1") String page,String[] list){
        System.out.println(Arrays.toString(list));

        int nums = 0;
        int sum = 0;

        if (StringUtils.isEmpty(searchName)){
            searchName = null;
        }
        String uuid = req.getSession().getAttribute("uid").toString();
        int uid = Integer.parseInt(uuid);
        List<CartVo> cartList = service.findAll(uid,searchName);
        for (int i = 0; i < list.length; i++) {
            if (list[i].equals("true")){
                nums += cartList.get(i).getNum();
                sum += cartList.get(i).getNum()*cartList.get(i).getPrice();
            }
        }
        
        Map map = new HashMap();
        map.put("nums",nums);
        map.put("sum",sum);
        return map;
    }
    
    
   
   
    @RequestMapping("/addToCart")
    @ResponseBody
    public String addToCart(String num,String pid,String price,
                       HttpServletRequest req){
        String uuid = req.getSession().getAttribute("uid").toString();
        Cart cart1 = new Cart();
        QueryWrapper<Cart> qw = new QueryWrapper<>();
        qw.eq("pid",Integer.parseInt(pid));
        Cart cart2 = cart1.selectOne(qw);
        if (cart2 == null){
            Cart cart = new CartVo();
            cart.setUid(Integer.parseInt(uuid));
            cart.setPid(Integer.parseInt(pid));
            cart.setPrice(Double.parseDouble(price));
            cart.setNum(Integer.parseInt(num));
            cart.insert();
        }
        req.setAttribute("searchName","");
       // return "redirect:goToCart";
        //return "cart";
        return "111";
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Map delete(String cid, HttpServletRequest req,@RequestParam(defaultValue = "")String searchName){
        Map map = new HashMap();
        map.put("searchName",searchName);
        
        
        
        Cart cart = new Cart();
        cart.setCid(Integer.parseInt(cid));
        cart.deleteById();


        if (StringUtils.isEmpty(searchName)){
            searchName = null;
        }else {
            searchName = "%" + searchName + "%";
        }
        String uuid = req.getSession().getAttribute("uid").toString();
        int uid = Integer.parseInt(uuid);
        List<CartVo> cartList = service.findAll(uid,searchName);
        
        map.put("cartList",cartList);
      
        return map;
        
    }
}

